import os
import json
from pymongo import MongoClient

# 连接到MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['LocalArkTSTest']
npm_functions_collection = db['npm_functions_testv2']
version_diff_function_info_collection = db['version_diff_function_info_testv2']
library_info_collection = db['library_info_testv2']

# 用于记录library不一致的情况
library_discrepancy_file = 'library_discrepancies.txt'

# 分批处理的大小
BATCH_SIZE = 5000


def process_project_folder(project_folder_path):
    project_name = None
    json_files = ['diff_function_info.json', 'history_no_diff_function.json',
                  'version_diff_function_mate.json', 'version_mate_info.json']

    for json_file in json_files:
        file_path = os.path.join(project_folder_path, json_file)
        if os.path.exists(file_path):
            with open(file_path, 'r', encoding='utf-8') as f:
                data = json.load(f)

            if json_file in ['diff_function_info.json', 'history_no_diff_function.json']:
                if not project_name:
                    project_name = data[0].get('library') if data else None
                if data:
                    # 分批插入npm_functions集合
                    for i in range(0, len(data), BATCH_SIZE):
                        batch = data[i:i + BATCH_SIZE]
                        npm_functions_collection.insert_many(batch)

            elif json_file == 'version_diff_function_mate.json':
                version_docs = []
                for version, version_data in data.items():
                    version_doc = {
                        'library': project_name,
                        'version': version,
                        'diff_function': version_data.get('diff_function', []),
                        'from_version': version_data.get('from_version')
                    }
                    version_docs.append(version_doc)
                # 分批插入version_diff_function_info集合
                for i in range(0, len(version_docs), BATCH_SIZE):
                    batch = version_docs[i:i + BATCH_SIZE]
                    version_diff_function_info_collection.insert_many(batch)

            elif json_file == 'version_mate_info.json':
                library_in_file = data.get('library')
                if project_name and project_name != library_in_file:
                    with open(library_discrepancy_file, 'a', encoding='utf-8') as f:
                        f.write(f'Project: {project_name}, File: {library_in_file}\n')
                library_info_collection.insert_one(data)


def main():
    result_folder = r'F:\result03'
    for folder_name in os.listdir(result_folder):
        folder_path = os.path.join(result_folder, folder_name)
        if os.path.isdir(folder_path):
            if folder_name.startswith('@'):
                # 组织文件夹，遍历里面的项目文件夹
                for sub_folder_name in os.listdir(folder_path):
                    sub_folder_path = os.path.join(folder_path, sub_folder_name)
                    if os.path.isdir(sub_folder_path):
                        process_project_folder(sub_folder_path)
            else:
                # 项目文件夹
                process_project_folder(folder_path)


if __name__ == '__main__':
    main()